package project.Dao;/*
 *  @Author Mr.long
 *  @QQ 1507468405
 *  @Creat 2021/10/26 - 11:34
 */

import Utils.JDBCTemp;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import project.Domain.User;

import java.util.List;

@SuppressWarnings("SqlResolve")
public class UserDao {
//    创建JdbcTemplate对象用于执行sql语句
    JdbcTemplate template = new JdbcTemplate(JDBCTemp.getDataSource());
    public User login(String username){
        String sql = "select * from xm05_user_file where username = ?";
        List<User> users = template.query(sql, new BeanPropertyRowMapper<>(User.class), username);
//        如果存在用户，由于用户名不能重名，所以用户列表的长度只能是1
        return users.size() == 1?users.get(0):null;
    }
    public int getLeaderID(int id){
        String sql = "select leaderID from xm05_user_file where id = ?";
        Integer leaderID = template.queryForObject(sql, Integer.class, id);
        return leaderID;
    }
    public String getNameById(int id){
        String sql = "select name from xm05_user_file where id = ?";
        String name = template.queryForObject(sql, String.class, id);
        return name;
    }
    public int getLeaderID(String username){
        String leader = "select leaderID from xm05_user_file where username = ?";
        String s = null;
        try {
            s = template.queryForObject(leader, String.class, username);
        }catch (EmptyResultDataAccessException e){
            return -1;
        }
        if(s==null || s.equals("0")){
            return -1;
        }
        return Integer.parseInt(s);
    }
    public List<User> getUserList(int leaderID){
        String sql = "select * from xm05_user_file where leaderID = ? ";
//        将查询的结果封装为一个userList集合返回
        List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class),leaderID);
        return list;
    }
//    增加已注册用户
    public boolean add(User user) {
        String sql = "insert into xm05_user_file(name,email,mobile,role,username,password,leaderID) VALUES(?,?,?,?,?,?,?)";
        int update = template.update(sql, user.getName(), user.getEmail(), user.getMobile(), user.getRole(),
                user.getUsername(), user.getPassword(),user.getLeaderID());
//        System.out.println(update);
        return update > 0;
    }


//    删除用户
    public boolean delete(int id){
        String sql = "delete from xm05_user_file where id = ?";
        int update = template.update(sql, id);
//        根据所影响的行数来判断是否删除成功
        return update > 0;
    }
    //    编辑用户
    public boolean edit(User user) {
        String sql = "update xm05_user_file set name = ?,email = ?,mobile = ?,role =? where id = ?";
        int update = template.update(sql, user.getName(), user.getEmail(), user.getMobile(), user.getRole(), user.getId());
        return update > 0;
    }
    //    编辑基本信息
    public boolean edit1(User user) {
        String sql = "update xm05_user_file set name = ?,email = ?,mobile = ?,username =? where id = ?";
        int update = template.update(sql, user.getName(), user.getEmail(), user.getMobile(), user.getUsername(), user.getId());
        return update > 0;
    }
    //    编辑基本信息
    public boolean edit3(User user) {
        String sql = "update xm05_user_file set leaderID =? where id = ?";
        int update = template.update(sql, user.getLeaderID(), user.getId());
        return update > 0;
    }
    //    修改密码
    public boolean updatePassword(int id, String new_password){
        String sql = "update xm05_user_file set password = ? where id = ?";
        int update = template.update(sql, new_password, id);
        return update > 0;
    }
//    新用户注册
    public boolean Register(User user){
        String sql = "insert into xm05_user_file(name,email,mobile,role,username,password,leaderID) VALUES(?,?,?,?,?,?,?)";
        int update = template.update(sql, user.getName(), user.getEmail(), user.getMobile(), user.getRole(),
                user.getUsername(), user.getPassword(),user.getLeaderID());
        return update > 0;
    }
///项目添加成功之后 更改组长的leaderID
    public boolean updateLeaderID(int id1, int id2){
        String sql = "update xm05_user_file set leaderID = ? where id = ?";
        int update = template.update(sql, id1, id2);
        return update > 0;
    }
    ///组长角色注销之后，组员的leaderID置空
    public boolean setLeaderIDNull(int id){
        String sql = "update xm05_user_file set leaderID = 0 where leaderID = ?";
        int update = template.update(sql, id);
        return update > 0;
    }

    public boolean hasMobile(String mobile){
        @SuppressWarnings("SqlResolve") String sql = "select mobile from xm05_user_file where mobile = ?";
        String s = null;
        try {
        s = template.queryForObject(sql, String.class, mobile);
        }catch (EmptyResultDataAccessException e){
            return false;
        }
        return s != null;
    }
    public boolean hasEmail(String email){
        String sql = "select email from xm05_user_file where email = ?";
        String s = null;
        try {
            s = template.queryForObject(sql, String.class, email);
        }catch (EmptyResultDataAccessException e){
            return false;
        }
        return s != null;
    }
    ////邮箱找回密码
    public List<User> getUserByEmail(String email){
        String sql = "select * from xm05_user_file where email = ? ";
//        将查询的结果封装为一个userList集合返回
        List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class),email);
        return list;
    }

    public String getMobileByID(int id) {
        String sql = "select mobile from xm05_user_file where id = ?";
        String s = null;
        try {
            s = template.queryForObject(sql, String.class, id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return s;
    }
}
